﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSoftSeo.Models;

namespace WebSoftSeo.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        WebSoftSeoEntities db = new WebSoftSeoEntities();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Register(User user)
        {
            User usertest = db.Users.Where(m => m.Email == user.Email).FirstOrDefault();
            if (usertest != null)
            {
                ViewBag.ErrorEmailR = "Email này đã được đăng ký";
                return View();
            }

            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                Session["Email"] = user.Email;
                Session["Username"] = user.FullName;
            }
            return RedirectToAction("index", "Home");
        }
       [HttpPost]
        public ActionResult Login(User user)
        {
            User usertest = db.Users.Where(m => m.Email == user.Email).FirstOrDefault();
            if (usertest == null)
            {
                ViewBag.ErrorEmail = "Email đăng nhập không đúng";
                return View("Register");
            }
            else
            {
                if (usertest.PassWord != user.PassWord)
                {
                    ViewBag.ErrorPassWord = "Mật khẩu đăng nhập không đúng";
                    return View("Register");
                }
                else
                {
                    Session["Email"] = usertest.Email;
                    Session["Username"] = usertest.FullName;
                }
            }
            return RedirectToAction("index", "Home");
        }
        public ActionResult Logout()
        {
            Session["Email"] = null;
            Session["Username"] = null;
            return RedirectToAction("index", "Home");
        }

    }
}
